package com.ebt.m.customer.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.ebt.m.R;
import com.ebt.m.customer.entity.PolicyBean;
import com.ebt.m.customer.event.PolicyUpdateEvent;
import com.ebt.m.customer.net.http.PolicyHttps;
import com.ebt.m.customer.net.json.ErrorJson;
import com.ebt.m.customer.net.json.PolicyAllJson;
import com.ebt.m.customer.ui.PolicyEditActivity;
import com.ebt.m.utils.UIHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 保单编辑
 * Created by archer.qi on 2016/12/16.
 */
public class OptionDialog implements AdapterView.OnItemClickListener {

    private Context mContext;
    private PopupWindowBuilder mBuilder;
    private PopupWindow mWindow;

    private PolicyBean mData;
    private ConfirmDialog mConfirmDialog;

    private static List<String> mListData = new ArrayList<>();

    static {
        mListData.add("编辑");
        mListData.add("删除");
    }

    public OptionDialog(Context context) {
        mContext = context;
        mBuilder = new PopupWindowBuilder(context)
                .fill(mListData)
                .listen(this);
        mWindow = mBuilder.create();
    }

    public void setData(PolicyBean policy) {
        mData = policy;
    }

    public void show(View view) {
        if (view != null && mWindow != null) {
            mWindow.showAsDropDown(view, -90 * 3, 0);
        }
    }

    public void dismiss() {
        if (mWindow != null) {
            mWindow.dismiss();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i) {
            case 0:
                // 编辑
                Intent intent = new Intent(mContext, PolicyEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(PolicyEditActivity.ARG_FLAG, 1);
                bundle.putSerializable(PolicyEditActivity.ARG_DATA, mData);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
                break;
            case 1:
                // 删除
                showConfirm();
                break;
        }
        if (mWindow != null) {
            mWindow.dismiss();
        }
    }

    private void showConfirm() {
        if (mConfirmDialog == null) {
            mConfirmDialog = new ConfirmDialog(mContext);
        }
        mConfirmDialog.setMessage("确定删除该保单吗？")
                .setPositiveButton("删除", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mConfirmDialog.dismiss();
                        deleteCustomerPolicy();
                    }
                }).show();
    }

    private void deleteCustomerPolicy() {
        if (mData != null) {
            PolicyHttps.getInstance(mContext.getApplicationContext()).deleteCustomerPolicy(mData.getPolicyId(), new PolicyHttps.onResponseCallback() {
                @Override
                public void onResponse(PolicyAllJson data) {
                    UIHelper.makeToast(mContext, "删除保单成功");
                    EventBus.getDefault().post(new PolicyUpdateEvent(2, data.policyId));
                }

                @Override
                public void onError(ErrorJson error) {
                    if (error != null) {
                        UIHelper.makeToast(mContext, mContext.getString(R.string.network_fail));
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    UIHelper.makeToast(mContext, mContext.getString(R.string.network_fail));
                }
            });
        }
    }
}
